﻿@{
    ViewBag.Title = "CommonDataExport";
    Layout = "~/Views/Shared/_GridCommonLayout.cshtml";
}






<div class="panel panel-success">
     <div class="panel-heading">通用查询及导出</div>
     <div class="panel-body" >
          <div class="form-group">
               <label for="select-1" class=" control-label font-md" style="width:90px; float:left;margin-top:1.5px">查询名称：</label>
                     <div class="col-md-6">
                          
                             <select id="select-1" class="form-control" data-bind="options: recordSet,optionsValue:'Id',optionsText: 'SSqlName'">
                      
                    </select>
                                </div>
              <button type="button" class="btn btn-success btn-labeled  "  data-bind="click:$root.QueryData">
                   
                    <span class="btn-label"><i class="fa fa-eye"></i>
                    </span>查询数据</button>
              <button type="button" class="btn btn-success btn-labeled " data-bind="click:$root.PostDataToExcel">
                   
                    <span class="btn-label"><i class="fa fa-arrow-circle-right"></i>
                    </span>导出Excel</button>
                     </div>
         <div class="form-group" style="display:none" id="ContentId">
             <table class="table table-bordered table-hover">
                 <thead>
                     <tr id="tableHeader"></tr>
                 </thead>
                 <tbody id="tableBody">
                     
                 </tbody>
             </table>
         </div>
          </div>
</div>

                                        




<script type="text/javascript">

    var gdViewModel;
    
    var headerlist = new Array;  //表头信息
    var contentlist = new Array; //表内容信息
    $(function () {
        
        



        function gridViewModel() {
            var self = this;


            self.recordSet = ko.observableArray(); //获取所有的SQL语句

            //初始化
            self.init = function () {
                self.SearchSQL();
            }
            self.initEd = function () {//结束初始化
            }

            //通用数据导出，收集所有的SQL语句名
            self.SearchSQL = function () {

                $.ajax(
                {

                    url: '/api/CommonDataExport/GetSQLName/',
                    type: "get",
                    success: function (result) {
                        if (result == null) {
                            result = "当前数据库不存在SQL查询语句";
                        }

                        self.recordSet(result);//收集返回的数据库表集

                    }
                });
            };



            //TO Add Method  

            //点击“查询数据”  按钮，根据所选的SQL语句，查出对应的数据
            self.QueryData = function ()
            {
                var reslut = $("#select-1").val(); //获取所选择的的SQL语句
                $("#tableHeader").empty();  //清空表头
                $.ajax({
                    url: '/api/CommonDataExport/GetSelectedData?id=' + reslut,
                    type: 'POST',
                    success: function (item){  //把取得的数据放到数据集中  todo
                        $("#ContentId").show();

                        contentlist = item.dataContent; //获取表内容信息
                        headerlist = item.dataHeader; //获取表头信息
                        $.each(headerlist, function (n, value) {
                            
                            $("#tableHeader").append('<td>'+value+'</td')
                        });
                        $("#tableBody").empty();
                        contentlist = item.dataContent; //获取表内容信息
                        $.each(contentlist, function (n, value) {
                            var rowsList = new Array;
                            rowsList = value;
                            var rows='';
                            $.each(rowsList, function (n, result) {
                                rows = rows + '<td>' + result + '</td>';
                            })
                            $("#tableBody").append('<tr>' + rows + '</tr')
                            
                        });
                       

                    }
                })
            }


            //点击“导出Excel”按钮，根据所选的SQL语句，导出到Excel
            self.PostDataToExcel = function () {
                var reslut = $("#select-1").val(); //获取所选择的的SQL语句
                
                $.download('/api/CommonDataExport/PostDataToExcel?id=' + reslut, 'find=commoncode', 'post');
            }





        }
       

        var gdViewModel = new gridViewModel();
        gdViewModel.init();
        ko.applyBindings(gdViewModel);





    });

</script>